import RoomsModel from '../dbs/models/rooms';
import uuid from 'node-uuid';
const uidv4 = uuid.v4();
import dtime from 'time-formater';

class RoomsController {
  constructor() {
    this.createRooms = this.createRooms.bind(this)
    this.updateRooms = this.updateRooms.bind(this)
  }

  async createRooms(ctx) {
    const { roomName, createUserName, createUserId } = ctx.request.body
    if (!roomName || !createUserName || !createUserId) {
      ctx.body = {
        code: 40001,
        message: '参数缺失',
        data: null
      }
    } else if (roomName.length > 12) {
      ctx.body = {
        code: 40001,
        message: '房间名称超出限制',
        data: null
      }
    } else {
      const uid = uidv4
      const rooms = {
        room_id: uid,
        room_name: roomName,
        create_user_name: createUserName,
        create_user_id: createUserId,
        create_time: dtime().format('YYYY-MM-DD HH:mm:ss'),
        status: 1
      }
      const room = RoomsModel.Rooms.create(rooms);
      const roomRes = await RoomsModel.Rooms.findOne({room_id: rooms.room_id});
      if (!roomRes) {
        ctx.body = {
          code: 50000,
          message: '创建房间失败',
          data: null
        }
      } else {
        ctx.body = {
          code: 20000,
          message: '创建成功',
          data: roomRes
        }
      }
    }
  }

  async updateRooms(ctx) {
    const {roomId, roomName, createUserId, createUserName } = ctx.request.body
    if(!roomId) {
      ctx.body = {
        code: 40001,
        message: '当前房间不存在',
        data: null
      }
    }
    if (roomName) {
      // await RoomsModel.Rooms.update({room_id: roomId}, {
      //   room_name: roomName,
      //   update_time: dtime().format('YYYY-MM-DD HH:mm:ss'),

      // })
    }
  }

}

export default new RoomsController()